<?php
    session_start();
    if (isset($_SESSION['userinfo']) or $_SESSION['userinfo']!="")
    {
        echo "<table width='99%' align='center'>";        
        echo "<tr><td style='text-align:left'>"."Hi, ".$_SESSION['userinfo']."</td>";
        echo "<td style='text-align:right'><a  href='../homebill/loginout.php'>Login out</a></td>";
        echo "</table>";    
    }
    else
    {
        echo "<script language='javascript'>";
        echo "self.location='login.html';";
        echo "</script>";
        exit();
    }
        
?>
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
    <meta http-equiv="Content-Type" content="application/x-www-form-urlencoded; charset=utf-8">
    <title>HomeBill</title>
    <link href="css/style.css" type="text/css" rel="stylesheet">
</head>
<?php
    include("connect.php");
    $longbill = array();
    $huibill = array();
    $weibill = array();
    $longnote = array();
    $huinote = array();
    $weinote = array();
    $even = array(0,0,0);
    $picname = array("long", "hui", "wei");
    $names = array($longbill, $huibill, $weibill);
    $notes = array($longnote, $huinote, $weinote);
    for ($i=0; $i <3 ; $i++) { 
        $result = mysql_query("SELECT * FROM detail WHERE Userid='$i'");
        while ($row = mysql_fetch_array($result)) {
            $str = $row['money']." ".$row['note'];
            array_unshift($names[$i], $row['money']);
            array_unshift($notes[$i], $str);
        }   
    }
?>
<body align="center">
    <?php
    echo "<div width='99%'>";
    $i = 0;
     foreach ($names as $varn) {
        foreach ($varn as $value) {
            $even[$i] += $value;
        }
        $i++;
    }
    $i=0;
    $total = round(($even[0]+$even[1]+$even[2])/3);
    foreach ($notes as $varn) {
        $even[$i] -= $total;
        echo "<div style='float:left; width:33%; align:center'>";
        echo "<table width='100%' border='0' align='center' padding='0'>";
        echo "<tr><td style='text-align:center;'><img src='../homebill/images/".$picname[$i].".jpg' width='50' height='50'></td>";
        echo "<hr>";
        if ($even[$i]<0) {
            echo "<tr><td style='color:red;text-align:center;'>".$even[$i]."</td>";
        }
        else echo "<tr><td style='color:green;text-align:center;'>".$even[$i]."</td>";
        foreach ($varn as $value) {
            echo "<tr><td style='text-align:left;border-left:1px solid black;'>".$value."</td>";

        }
        echo "</table>";    
        echo "</div>";
        $i++;
    }
    echo "</div>";
    ?>

    <div class="content">
        <hr>
        <div class="post">
            <form action="account.php" method="post">
                <label >New item:</label>
                <label >e.g.,</label>
                <label style='color:red'>30  买菜</label><br>
                <input type="text" class="text" name="Account" ><br>
                <input type="submit" value="Add" class="submit">
            </form>
        </div>
    </div>
</body>
</html>